package com.comm.update;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.io.IOException;

/**
 * Created by chenxunlin01 on 2017/10/9.
 */

public class Utils {

    /**
     * 判断本地是否有已下载的app包
     *
     * @param updateInfo
     * @return
     */
    public static String checkUpdateAppExist(Context context, UpdateInfo updateInfo) {
        String path = getDownloadFilePath(context, updateInfo.appSize);
        if(path == null){
            return null;
        }
        File file = new File(path);
        if (file.exists()) {
            String fileMd5 = SecurityUtil.getInstance().getMessageMd5(path);
            if (updateInfo.md5.equals(fileMd5)) {
                return path;
            }
        }
        return null;
    }

    /**
     * 获取app下载的完整路径
     *
     * @return
     */
    public static String getDownloadFilePath(Context context, long appSize) {
        long fileSize = appSize + (1024 << 10);
        if (externalMemoryAvailable() && (getAvailableExternalMemorySize() > fileSize)) {
            return Environment.getExternalStorageDirectory().getPath() + File.separator + Constant.downloadDir + File
                    .separator + Constant.downAppFileName;
        } else if (getAvailableInternalMemorySize() > fileSize) {
            return context.getFilesDir().getPath() + File.separator + Constant.downloadDir + File.separator +
                    Constant.downAppFileName;
        } else {
            return null;
        }
    }

    /**
     * 可用内存大小
     *
     * @param fileSize
     * @return
     */
    public static boolean memoryAvailable(long fileSize) {
        fileSize += (1024 << 10);
        if (externalMemoryAvailable()) {
            if ((getAvailableExternalMemorySize() <= fileSize)) {
                if ((getAvailableInternalMemorySize() > fileSize)) {
//                    createFile(false);
                    return true;
                } else {
                    return false;
                }
            } else {
//                createFile(true);
                return true;
            }
        } else {
            if (getAvailableInternalMemorySize() <= fileSize) {
                return false;
            } else {
//                createFile(false);
                return true;
            }
        }
    }

    // 获取内部存储器有用空间大小
    public static long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

    // 判断SD卡是否存在
    public static boolean externalMemoryAvailable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    // 获取SD卡有用空间大小，错误返回-1
    public static long getAvailableExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return -1;
        }
    }

    /**
     * 创建文件
     * @param path
     * @return
     */
    public static File createFile(String path) throws IOException {
        File file = new File(path);
        file.getParentFile().mkdirs();
        file.createNewFile();
        return file;
    }

    /**
     * 删除文件
     * @param path
     */
    public static void delFile(String path){
        File file = new File(path);
        if(file.exists()){
           file.delete();
        }
    }
}
